<template>
  <el-dialog
    :title="!dataForm.applyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="id">
      <el-input v-model="dataForm.id" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="carId">
      <el-input v-model="dataForm.carId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="carName">
      <el-input v-model="dataForm.carName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userName">
      <el-input v-model="dataForm.userName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="carLicense">
      <el-input v-model="dataForm.carLicense" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="applyBool">
      <el-input v-model="dataForm.applyBool" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          applyId: 0,
          id: '',
          carId: '',
          carName: '',
          userName: '',
          carLicense: '',
          userPhone: '',
          applyBool: ''
        },
        dataRule: {
          id: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          carId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          carName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          carLicense: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          applyBool: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.applyId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.applyId) {
            this.$http({
              url: this.$http.adornUrl(`/car/apply/info/${this.dataForm.applyId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.apply.id
                this.dataForm.carId = data.apply.carId
                this.dataForm.carName = data.apply.carName
                this.dataForm.userName = data.apply.userName
                this.dataForm.carLicense = data.apply.carLicense
                this.dataForm.userPhone = data.apply.userPhone
                this.dataForm.applyBool = data.apply.applyBool
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/car/apply/${!this.dataForm.applyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'applyId': this.dataForm.applyId || undefined,
                'id': this.dataForm.id,
                'carId': this.dataForm.carId,
                'carName': this.dataForm.carName,
                'userName': this.dataForm.userName,
                'carLicense': this.dataForm.carLicense,
                'userPhone': this.dataForm.userPhone,
                'applyBool': this.dataForm.applyBool
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
